#include"Commond.h"
#include"CentralCache.h"
#include"ThreadCache.h"
#include"PageCache.h"
#include"ConcurrnetAlloc.h"
#include<iostream>
#include<thread>
using namespace std;
void Text1()
{
	void * ptr1=ConcurrentAlloc(4);
	void* ptr2 = ConcurrentAlloc(4);
	void* ptr3 = ConcurrentAlloc(4);
	void* ptr4 = ConcurrentAlloc(4);
	std::cout << ptr1 << std::endl;
	std::cout << ptr2 << std::endl;
	std::cout << ptr3 << std::endl;
	std::cout << ptr4 << std::endl;

	ConcurrentFree(ptr1, 4);
	ConcurrentFree(ptr2, 4);
	ConcurrentFree(ptr3, 4);
	ConcurrentFree(ptr4, 4);
}
void TextThread1()
{
	void* ptr1 = ConcurrentAlloc(4);
	void* ptr2 = ConcurrentAlloc(4);
	void* ptr3 = ConcurrentAlloc(4);
	std::cout << ptr1 << std::endl;
	std::cout << ptr2 << std::endl;
	std::cout << ptr3 << std::endl;
	ConcurrentFree(ptr1, 4);
	ConcurrentFree(ptr2, 4);
	ConcurrentFree(ptr3, 4);
}
void TextThread2()
{
	void* ptr1 = ConcurrentAlloc(12); 
	void* ptr2 = ConcurrentAlloc(12);
	void* ptr3 = ConcurrentAlloc(12);
	std::cout << ptr1 << std::endl;
	std::cout << ptr2 << std::endl;
	std::cout << ptr3 << std::endl;
	ConcurrentFree(ptr1, 12);
	ConcurrentFree(ptr2, 12);
	ConcurrentFree(ptr3, 12);
}
void TextThread3()
{
	void* ptr1 = ConcurrentAlloc(20);
	void* ptr2 = ConcurrentAlloc(20);
	void* ptr3 = ConcurrentAlloc(20);
	std::cout << ptr1 << std::endl;
	std::cout << ptr2 << std::endl;
	std::cout << ptr3 << std::endl;
	ConcurrentFree(ptr1, 20);
	ConcurrentFree(ptr2, 20);
	ConcurrentFree(ptr3, 20);
}
void MultiThreadAlloc2()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 10000; ++i)
	{
		void* ptr = ConcurrentAlloc(16);
		v.push_back(ptr);
	}

	for (auto e : v)
	{
		cout << e << std::endl;
		ConcurrentFree(e, 16);
	}
}
void BigAlloc()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 8; ++i)
	{
		void* ptr = ConcurrentAlloc(257*1024);
		v.push_back(ptr);
	}

	for (auto e : v)
	{
		cout << e << std::endl;
		ConcurrentFree(e, 257*1024);
	}
}
void BigAlloc1()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 2; ++i)
	{
		void* ptr = ConcurrentAlloc(129 *4* 1024);
		v.push_back(ptr);
	}

	for (auto e : v)
	{
		cout << e << std::endl;
		ConcurrentFree(e, 129 * 4 * 1024);
	}
}
int main()
{
	//MultiThreadAlloc2();
	//Text1();
	std::thread t1(BigAlloc1);
	std::thread t2(BigAlloc1);
	std::thread t3(BigAlloc1);

	t1.join();
	t2.join();
	t3.join();
	
	return 0;
}